<template>
    <div class="input-daterange input-group" :id="id" :class="[myClass]">
        <DateTimePicker :readonly="false" v-model="sd" :min-view="minView" :format="format" :end-date="int_time"></DateTimePicker>
        <span class="input-group-addon">至</span>
        <DateTimePicker :readonly="false" v-model="ed" :min-view="minView" :format="format" :end-date="int_time"></DateTimePicker>
    </div>
</template>

<script>    
    import DateTimePicker from './DateTimePicker.vue'

    export default {
        name: 'DateTimePickerRange',
        components:{DateTimePicker},
        data() {
            return {
                id:'dpr-'+Math.random().toString().split('.')[1],
                sd:'',
                ed:'',
                int_time:(new Date().getFullYear())+'-'+(new Date().getMonth()+1)+'-'+(new Date().getDate())+' 23:59:59',
            }
        },
        props:{
            value:{
                type:Object,
                default:{
                    sd:'',
                    ed:''
                }
            },
            myClass:{
                type:String,
                default:''
            },
            sname:{
                type:String,
                default:'start-date'
            },
            ename:{
                type:String,
                default:'end-date'
            },
            endDate:{
                type:String,
                default:'0d'
            },
            minView:{
                type:String,
                default:'hour'
            },
            format:{
                type:String,
                default:'yyyy-mm-dd hh:ii'
            }
        },
        watch:{
            value:{
                deep:true,
                handler(v,ov){
                    var self = this;
                    self.sd = v.sd;
                    self.ed = v.ed;
                }
            },
            sd(v,ov){
                var self = this;
                if(v!=''){
                    var sd = new Date(v);
                    if(self.ed!=''){
                        var ed = new Date(self.ed);
                        if(sd>ed){
                            self.ed = v;
                        }
                    } else {
                        self.ed = v;
                    }
                }
                self.$emit('input',{sd:v,ed:self.ed});
            },
            ed(v,ov){
                var self = this;
                if(v!=''){
                    var ed = new Date(v);
                    if(self.sd!=''){
                        var sd = new Date(self.sd);
                        if(sd>ed){
                            self.sd = v;
                        }
                    } else {
                        self.sd = v;
                    }
                }
                self.$emit('input',{sd:self.sd,ed:v});
            }
        },
        methods: {
            
        },
        mounted() {
            var self = this; 
            
        }
    }
</script>

<style scoped>

</style>